package com.tanhua.server.controller;

import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 评论-提交
     *
     * @return
     */
    @PostMapping
    public ResponseEntity comments(@RequestBody Map map) {
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentService.comments(movementId, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论查询
     *
     * @param movementId
     * @param page
     * @param pageseze
     * @return
     */
    @GetMapping
    public ResponseEntity findComments(String movementId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pageseze) {
        PageResult pr = commentService.findComments(movementId, page, pageseze);
        return ResponseEntity.ok(pr);
    }


    /**
     * 评论点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity pingLunLike(@PathVariable("id") String movementId){
        Integer likeCount = commentService.pingLunLike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消评论点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String movementId){
        Integer likeCount = commentService.disLike(movementId);
        return ResponseEntity.ok(likeCount);
    }
}
